<?php
    session_start();
    require_once '../includes/runtime/lib/Propel.php';
    Propel::init("../includes/build/conf/easylib-conf.php");
    set_include_path("../includes/build/classes" . PATH_SEPARATOR . get_include_path());
    $email=$_POST['email'];
    $id=$_POST['id'];
    $user=  TabAbonneQuery::create()->filterByEmail($email)->findOne();
    $isAbonneLicence=  TabLicenceQuery::create()->filterByFkAbonne($user->getIdAbonne())->orderByDateFin(Criteria::DESC)->findOne();
    $d1=date("Y-m-d H:i:s");
    if(count($isAbonneLicence)>0){
        if(strcmp($isAbonneLicence->getDateFin($format="Y-m-d H:i:s"),$d1)>0){
            $station = TabStationQuery::create()->filterByIdStation($id)->findOne();
            $bicyclette= TabBycicletteQuery::create()->filterByFkStation($station->getIdStation())->filterByStatusByciclette(0)->findOne();
            $stationsup = TabInformationStationQuery::create()->filterByFkStation($station->getIdStation())->findOne();
            if($stationsup->getAvailableBikes()>0){
                $utilisation = new TabUtilisation();
                $utilisation->setFkAbonne($user->getIdAbonne());
                $utilisation->setFkStationDepart($station->getIdStation());
                $utilisation->setFkByciclette($bicyclette->getIdByciclette());
                $utilisation->setDateDebut(new DateTime());
                $utilisation->save();
                $bicyclette->setStatusByciclette(1);
                $bicyclette->save(); 
                $stands=$stationsup->getAvailableBikeStands();
                $velos=$stationsup->getAvailableBikes();
                $stationsup->setAvailableBikeStands($stands+1);
                $stationsup->setAvailableBikes($velos-1);
                echo('0');
            }else{
                echo('-1');
            }
        }else{
            echo('1');
        }
    }else{
        echo('1');
    }
?>
